package com.yonyou.pmclouds.siteinspection.process;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.yonyou.pmclouds.basecom.bp.processor.SingleProcessor;
import com.yonyou.pmclouds.basecom.consts.BillTypeConst;
import com.yonyou.pmclouds.basecom.exception.BusinessException;
import com.yonyou.pmclouds.procedure.entity.ProcedureVO;
import com.yonyou.pmclouds.procedure.rmiitf.ProcedureQuery;
import com.yonyou.pmclouds.projectlog.entity.InspectionContentVo;
import com.yonyou.pmclouds.projectlog.entity.ProjectLogDetailVo;
import com.yonyou.pmclouds.projectlog.entity.ProjectLogJobVo;
import com.yonyou.pmclouds.projectlog.rmiitf.ProjectLogMaintain;
import com.yonyou.pmclouds.siteinspection.entity.SiteInspectionVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.text.MessageFormat;
import java.util.Calendar;

/**
 * 智能日志记录维护后置处理器，新建或更新都使用
 * 出错不要影响其他本来的流程
 * @author xiejianyu
 * 2019-09-23
 */
@Slf4j
@Component
public class SiteInspectionProjectLogPostProcess implements SingleProcessor<SiteInspectionVO> {

    private static ObjectMapper objectMapper = new ObjectMapper();
    @Autowired
    private ProjectLogMaintain projectLogMaintain;
    @Autowired
    private ProcedureQuery procedureQuery;

    @Override
    public void process(SiteInspectionVO vo, SiteInspectionVO orig) throws BusinessException {
        //如果使用标准的 SingleInsert SingleUpdate SingleDelete 模板进行操作的话，新建时orig 为null，更新或删除时orig不为null
        try {
            if (orig==null) {//目前只有新增记录

                //只有固定几个单据类型才有智能日志
                if (billTypeNotFit(vo.getBill_type())) {
                    return;
                }

                ProjectLogJobVo jobVo = new ProjectLogJobVo();//最终放入队列的任务对象

                String procedureName = "";
                if (StringUtils.isEmpty(vo.getPk_process())) {
                    String billType = vo.getBill_type();
                    if (BillTypeConst.PARALLEL_INSPECTION_BILLTYPE.equals(billType)) {
                        procedureName = "平行检验";
                    } else if (BillTypeConst.SIDE_INSPECTION_BILLTYPE.equals(billType)) {
                        procedureName = "旁站记录";
                    } else if (BillTypeConst.PATROL_BILLTYPE.equals(billType)) {
                        procedureName = "巡查记录";
                    }
                } else {
                    ProcedureVO procedureVO = procedureQuery.getById(vo.getPk_process());
                    procedureName = procedureVO.getSname();
                }

                String summary = MessageFormat.format(InspectionContentVo.summaryTemplate, vo.getInspectarea(), procedureName);

                InspectionContentVo inspectionContentVo = new InspectionContentVo();
                inspectionContentVo.setPkBill(vo.getPk_siteinspection());
                inspectionContentVo.setBillType(vo.getBill_type());
                inspectionContentVo.setSummary(summary);
                //合格率
                inspectionContentVo.setQualifiedRate(vo.getQualifiedRate().toString());

                /* 只新建头单据，没有通过数量
                inspectionContentVo.setTotalItems();
                inspectionContentVo.setPassedItems();
                */

                ProjectLogDetailVo logDetail = new ProjectLogDetailVo();
                logDetail.setDetailType(ProjectLogDetailVo.INSPECTION_CONTENT);
                logDetail.setPkContentSource(vo.getPk_siteinspection());
                logDetail.setContentJson(objectMapper.writeValueAsString(inspectionContentVo));
                logDetail.setCreator(vo.getCreator());
                logDetail.setCreationtime(vo.getCreationtime());
                logDetail.setModifier(vo.getCreator());
                logDetail.setModifytime(vo.getCreationtime());

                jobVo.setPkTenant(vo.getPk_tenant());
                jobVo.setPkProject(vo.getPk_project());

                Calendar calendar = Calendar.getInstance();
                calendar.setTime(vo.getCreationtime());
                calendar.set(Calendar.HOUR_OF_DAY, 0);
                calendar.set(Calendar.MINUTE, 0);
                calendar.set(Calendar.SECOND, 0);
                calendar.set(Calendar.MILLISECOND, 0);

                jobVo.setLogDate(calendar.getTime());
                jobVo.setEventType(ProjectLogJobVo.EVENT_TYPE_DETAIL);
                jobVo.setEventTime(vo.getCreationtime());
                jobVo.setUserId(vo.getCreator());
                jobVo.setAttachment(logDetail);
                //pmCloudCacheManager.addObjToBlockingQueue(ProjectLogJobVo.JOB_QUEUE_NAME, jobVo);
                projectLogMaintain.upsert(jobVo);
            }
        } catch (Exception e) {
            log.error("现场检查，子类型code：{} --智能日志处理出错，记录pk：{}", vo.getBill_type(), vo.getPk_siteinspection());
            log.error("异常信息", e);
        }
    }

    private boolean billTypeNotFit(String billType){
        if (billType==null) {
            return true;
        }

        //平行检验
        if (billType.equals(BillTypeConst.PARALLEL_INSPECTION_BILLTYPE)) {
            return false;
        }

        //旁站记录
        if (billType.equals(BillTypeConst.SIDE_INSPECTION_BILLTYPE)) {
            return false;
        }

        //巡查记录
        if (billType.equals(BillTypeConst.PATROL_BILLTYPE)) {
            return false;
        }

        return true;
    }

}